var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var reload = true;
var page = 1;
var hasMore = true;
var loadingMore = false;

var SDKVersion = '';
var status = true;

var inviteuid = '';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: app.globalData.imgDomain,

    classlysData: '',

    zxsData: [],
    hasData: true,

    uid: 0,
    token: '',
    openid: '',
    canEdit: 0,

    bottom_nav_data: [],
    isIphone_x: false,
    nav_bgColor: '#ffffff',

    shouqian_phone: '',
    colorSet: '',
    wz_item_img_w_1: 300,

    hide_commentview: true,

    gametu: [], //广告图

    paimingData: [],

    userinfo: {},

    array:[],
    hasShare: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("111111111111",options);
    var array = JSON.parse(options.array);
    console.log("array", array);
    this.setData({
      array : array
    });
    var tc = app.get_tc();
    var titlename = tc == 1 ? '助力好友' : '助力好友';
    wx.setNavigationBarTitle({
      title: titlename,
    });
    wx.showLoading({
      title: '加载中',
      mask: 'true'
    })

    page = 1;
    hasMore = true;
    loadingMore = false;

    var that = this;

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
    });

    //初始化底部导航
    var b_nav_data = app.getBottomNavData('zixune/zixune', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor,
      });
    });

    wx.getSystemInfo({
      success: function (res) {
        SDKVersion = res.SDKVersion;
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          tc: app.get_tc(),
          isIphone_x: isIphone_x,
          wz_item_img_w_1: res.windowWidth - 25
        });
      }
    })
    //是否是从分享进来的
    inviteuid = array.inviteuid ? array.inviteuid : '';

    var pages = getCurrentPages();
    var curPageIndex = pages.length;
    //如果是分享进来 或者 当前栈下标 是 1 都认为是分享进来的  因为分享人可能没授权登录 无法分享邀请码
    var isShare = (inviteuid || curPageIndex <= 1);
    this.setData({
      hasShare: isShare,
    })
    //当前登录用户信息
    app.LoginUserInfo(function (res) {
      var authFail = res == -1;//用户拒绝授权
      if (!authFail) {
        //更新数据
        that.setData({
          uid: res.uid,
          token: res.token,
          openid: res.wxsmall_openid
        });
        util.api({
          data: {
            "uid": res.uid,
            "publickey": util.publiccode,
            "ac": "userinfo"
          },
          success: function (responseData) {
            console.log(responseData.data);
            that.setData({
              userinfo: responseData.data
            });
          }
        });
      }

    }, inviteuid);
    //获取关于等信息
    app.getAllAbout('shouqian', function (res) {
      that.setData({
        shouqian_phone: res
      })
    });
     wx.hideLoading();
    this.getFriendsHelp();
    this.getGametu();
  },

  //判断用户有没有帮助过当前好友助力
  getFriendsHelp:function(){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "getFriendsHelp",
          "uid": that.data.uid,
          "orgid": util.orgid,
          "inviteuid":inviteuid
        },
        success: function (responseData) {
          console.log("判断结果：", responseData.data);

        } 
      });
  },


 

  loadMore: function (e) {
    if (hasMore && !loadingMore) {
      loadingMore = true;
      page++;
      // this.loadZXS(page);
    } else {
      console.log("没有更多了");
    }
  },

 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      wx.redirectTo({ url: path })
    }
  },



  //页面分享
  onShareAppMessage: function (res) {

    var that = this;
    var array = that.data.array;
      

      var option = JSON.stringify(array);
      console.log(option);
      console.log("分享id" + option);
    return {
      title: '助力好友获得更多奖励！！！',
      path: '/pages/my-help/my-help?&array=' + option
    };
  },


  //获取广告图
  getGametu: function () {
    var that = this;
    util.api({
      data: {
        "uid": that.data.uid,
        "orgid": util.orgid,
        "publickey": util.publiccode,
        "ac": "getGametu"
      },

      success: function (responseData) {
        console.log("获取广告图", responseData.data);
        that.setData({
          gametu: responseData.data
        })
      }
    });
  },
})